<template>
  <div class="app-container">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm"
             style="width: 50%; padding: 30px 30px;">
      <el-form-item label="新密码" prop="pass">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"
                  placeholder="新密码长度请大于6位小于15位"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" placeholder="请再输入一遍新密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { updateApi } from './my-api'

  export default {
    name: 'TmptAuthUser-MyFormDetail-Password',
    computed: {
      ...mapGetters([
        'user'
      ])
    },

    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'))
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass')
          }
          callback()
        }
      }
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'))
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      }
      return {
        ruleForm: {
          pass: '',
          checkPass: ''
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }, { min: 6, max: 15, message: '长度在6~15个字符', trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }, { min: 6, max: 15, message: '长度在6~15个字符', trigger: 'blur' }
          ]
        },
        temp: {}
      }
    },

    methods: {
      submitForm() {
        let userId = this.user.userId
        let pass = this.ruleForm.pass
        this.$refs['ruleForm'].validate((valid) => {
          if (valid) {
            const param = { id: userId }
            param.passwordBcrypt = pass
            updateApi(param).then((r) => {
              this.notify(r)
              if (r.success) {
                this.resetTemp()
              }
            })
          }
        })

      },

      resetForm() {
        this.$refs['ruleForm'].resetFields()
      }

    }
  }
</script>
<style>

</style>
